package cxm.proj.receiver;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.RandomAccessFile;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import Ice.Current;
import cap.Image;
import cap._ReceiverDisp;

public class CaptureReceiver extends _ReceiverDisp {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String[] getImageList(String token, Current __current) {
		System.out.println("get image with token " + token);
		
		if (null == token || "" == token)
			return new String[0];
		
		// read folder file count
		File readFolder = new File(token);
		if (!readFolder.exists() || !readFolder.isDirectory()) {
			System.out.println("Cannot find folder");
			return new String[0];
		}
		
		File []pngFiles = readFolder.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File arg0, String arg1) {
				return arg1.toLowerCase().endsWith("png");
			}
		});
		
		String []pngStrings = new String[pngFiles.length];
		for (int i = 0; i < pngFiles.length; i++)
			pngStrings[i] = pngFiles[i].getName();
		
		return pngStrings;
	}

	@Override
	public int uploadImage(String token, Image imageData, Current __current) {
		if (null == token || "" == token || 
				imageData.imageData == null || imageData.imageData.length <= 0 ||
				null == imageData.location || "" == imageData.location) {
			System.out.println("Invalid input");
			return -1;
		}
		
		// create folder
		File saveFolder = new File(token);
		if (!saveFolder.exists())
			if (!saveFolder.mkdir()) {
				System.out.println("Cannot make dir " + token);
				return -2;
			}
		
		// save image with location name
		// current time
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		//get current date time with Date()
		Date date = new Date();
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(token + "/" + imageData.location +
					"_" + dateFormat.format(date) + ".png");
			fos.write(imageData.imageData);
			fos.close();
		} catch (Exception e) {
			System.out.println("Cannot save file: " + e);
			return -3;
		} finally {
			if (null != fos)
				try {
					fos.close();
				} catch (Exception e) {
					System.out.println("Cannot close file: " + fos.toString());
				}
		}
		
		return 0;
	}

	@Override
	public Image downloadImage(String token, String imageName, Current __current) {
		if (null == token || "" == token ||
				null == imageName || "" == imageName)
			return null;
		
		System.out.println("get image with token: " + token + " name: " + imageName);
		// read image
		File readFile = new File(token + "/" + imageName);
		if (!readFile.exists() || !readFile.isFile()) {
			System.out.println("Cannot read file: " + imageName);
			return null;
		}
		RandomAccessFile randomFileStream = null;
		byte []imageBytes = null;
		try {
			randomFileStream = new RandomAccessFile(readFile, "r");
			imageBytes = new byte[(int)randomFileStream.length()];
			randomFileStream.read(imageBytes);
		} catch (Exception e) {
			System.out.println("Cannot read file: " + e);
			return null;
		} finally {
			if (null != randomFileStream)
				try {
					randomFileStream.close();
				} catch (Exception e) {
					System.out.println("Cannot close file: " + e);
				}
		}
		
		// construct return value
		Image image = new Image();
		image.location = imageName.split("_")[0];
		image.imageData = imageBytes;
		
		return image;
	}
}
